package com.gw.smscheck.service.sys.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gw.smscheck.dao.sys.MenuMapper;
import com.gw.smscheck.entity.sys.*;
import com.gw.smscheck.service.sys.MenuService;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理业务层
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    private Logger logger = Logger.getLogger(MenuServiceImpl.class);

    /**
     * 菜单列表信息
     * @param
     * @return
     */
    @Override
    public  List<Menu> searchList(Menu dto) {
        List< Menu> resultList = new ArrayList<>();
        PageHelper.startPage(dto.getPage(), dto.getRows(), "sort asc");
        resultList = menuMapper.searchList(dto);
        return resultList;
    }
    /**
     * 添加
     * @param
     * @return
     */
    @Override
    public Integer add(Menu menu) {

        return menuMapper.add(menu);
    }
    /**
     * 修改
     * @param
     * @return
     */
    @Override
    public Integer update(Menu menu) {
        return menuMapper.update(menu);
    }
    /**
     * 删除
     * @param
     * @return
     */
    @Override
    public Integer del(Menu menu) {
        return menuMapper.del(menu);
    }

    /**
     * 授权菜单查询
     * @Title: queryAllSysMenus
     * @param @throws Exception
     * @return List<TreeModel>
     * @throws
     */
    public List<TreeModel> queryAllSysMenus(Menu m)throws Exception
    {
        // 获取所有的一级菜单
        List<Menu> menuInfos = menuMapper.findAllMenuInfos(m);
        logger.info("sads:" + menuInfos);
        List<TreeModel> result = new ArrayList<>();
        TreeModel model = null;
        for (Menu menu : menuInfos)
        {
            model = new TreeModel();
            if (menu.getPid() == 0)
            {
                model.setId(menu.getId());
                model.setText(menu.getTitle());
                if (menu.getMenuId() != null)
                {
                    model.setChecked(true);
                }
                else
                {
                    model.setChecked(false);
                }
                // 计算孩子节点：下级菜单
                Menu parent = new Menu();
                parent.setPid(menu.getId());
                parent.setRoleId(m.getRoleId());
                List<Menu> childrens = menuMapper.findAllMenuInfos(parent);
//                logger.info("model:" + model.toString());
//                logger.info("childrens:" + childrens);
                if (!childrens.isEmpty())
                {
                    model.setChildren(getChildren(parent));
                }
                result.add(model);
            }
        }
        logger.info("菜单集合：" + result);
        return result;
    }

    /**
     * 授权菜单查询
     * @Title: queryAllSysMenus
     * @param @throws Exception
     * @return List<TreeModel>
     * @throws
     */

    /**
     * 递归获取下级所有的子菜单
     * @param parent
     * @throws Exception
     */
    private List<TreeModel> getChildren(Menu parent) throws Exception
        {
        List<TreeModel> result = new ArrayList<>();
        TreeModel model = null;
        List<Menu> menuInfos = menuMapper.findAllMenuInfos(parent);
        for (Menu menu : menuInfos)
        {

            model = new TreeModel();
            model.setId(menu.getId());
            model.setText(menu.getTitle());
            model.setTitle(menu.getTitle());
            model.getAttributes().put("url", menu.getUrl());
            if (menu.getMenuId() != null)
            {
                model.setChecked(true);
            }
            else
            {
                model.setChecked(false);
            }
            // 计算孩子节点：下级菜单
            Menu son = new Menu();
            son.setPid(menu.getId());
            son.setRoleId(menu.getRoleId());
            List<Menu> childrens = menuMapper.findAllMenuInfos(son);
            if (!childrens.isEmpty())
            {
                List<TreeModel> nbc = getChildren(son);
                logger.info("子节点：" + nbc);
                if (!nbc.isEmpty())
                {
                    model.setChildren(nbc);
                }
                else
                {
                    model.setChildren(null);
                }
            }
            result.add(model);
        }
        logger.info("返回集合" + result);
        return result;
    }

    /**
     * 获取动态菜单
     */
    public List<TreeModel> searchAllSysMenus(Menu m)throws Exception
    {
        // 获取所有的一级菜单
        List<Menu> menuInfos = menuMapper.findAllMenuInfos(m);
        logger.info("sads:" + menuInfos);
        List<TreeModel> result = new ArrayList<>();
        System.out.println("1111111111111==========="+result);
        TreeModel model = null;
        for (Menu menu : menuInfos)
        {
            model = new TreeModel();
            if (menu.getPid() == 0)
            {
                model.setUrl(menu.getUrl());
                model.setId(menu.getId());
                model.setIconurl(menu.getIconurl());
                model.setText(menu.getTitle());
                if (menu.getMenuId() != null)
                {
                    model.setChecked(true);
                }
                else
                {
                    model.setChecked(false);
                }
                // 计算孩子节点：下级菜单
                Menu parent = new Menu();
                parent.setPid(menu.getId());
                parent.setRoleId(m.getRoleId());
                List<Menu> childrens = menuMapper.findAllMenuInfos(parent);
//                logger.info("model:" + model.toString());
//                logger.info("childrens:" + childrens);
                if (!childrens.isEmpty())
                {
                    model.setChildren(searchChildren(parent));
                }
                result.add(model);
            }
        }
        logger.info("菜单集合：" + result);
        List<TreeModel> resultList = new ArrayList<>();
        if(result!=null && result.size()>0){
            for (int i=0; i<result.size();i++){
                final boolean checked = result.get(i).isChecked();
                if (checked){
                    resultList.add(result.get(i));
                }

            }
        }
        return resultList;
    }
    private List<TreeModel> searchChildren(Menu parent) throws Exception
    {
        List<TreeModel> result = new ArrayList<>();
        TreeModel model = null;
        List<Menu> menuInfos = menuMapper.findAllMenuInfos(parent);
        for (Menu menu : menuInfos)
        {

            model = new TreeModel();
            model.setId(menu.getId());
            model.setIconurl(menu.getIconurl());
            model.setText(menu.getTitle());
            model.setTitle(menu.getTitle());
            model.setUrl(menu.getUrl());

            if (menu.getMenuId() != null)
            {
                model.setChecked(true);
            }
            else
            {
                model.setChecked(false);
            }
            // 计算孩子节点：下级菜单
            Menu son = new Menu();
            son.setPid(menu.getId());
            son.setRoleId(menu.getRoleId());
            List<Menu> childrens = menuMapper.findAllMenuInfos(son);
            if (!childrens.isEmpty())
            {
                List<TreeModel> nbc = getChildren(son);
                logger.info("子节点：" + nbc);
                if (!nbc.isEmpty())
                {
                    model.setChildren(nbc);
                }
                else
                {
                    model.setChildren(null);
                }
            }
            result.add(model);
        }
        logger.info("返回集合" + result);
        List<TreeModel> resultList = new ArrayList<>();
        if(result!=null && result.size()>0){
            for (int i=0; i<result.size();i++){
                final boolean checked = result.get(i).isChecked();
                if (checked){
                    resultList.add(result.get(i));
                }

            }
        }
        return resultList;
    }
    /**
     * 保存角色权限
     * @Title: saveRoleAndPower
     * @param rId
     * @param  rightIds
     * @return boolean
     * @throws
     */
    public boolean saveRoleAndPower(Integer rId, Integer[] rightIds)
    {
        try
        {
            //1、删除该角色的权限
            this.menuMapper.clearRolePower(rId);
            //2、重新添加权限：提升清除所有权限的性能
            if (rightIds != null && rightIds.length > 0)
            {
                Map<String, Object> data = new HashMap<>();
                data.put("rId", rId);
                data.put("rightIds", rightIds);
                this.menuMapper.authorPower(data);
            }
            return true;
        }
        catch (Exception e)
        {
            logger.info("异常了");
        }
        return false;
    }
}